package com.jinzhi.jzweb.utils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author mengzhichao
 * @create 2023-02-23-8:51
 */
public class IDUtil {
    private SimpleDateFormat sfd = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private Set<Integer> randomSet = new HashSet<Integer>();
    private String dateFlag;
    private int sequence;

    /**
     *   初始化  默认(默认是时间格式和序列)
     * @return
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:45
     */
    public IDUtil() {
        this.sfd = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        this.dateFlag = this.sfd.format(new Date());
        this.sequence = 0;
    }

    /**
     *   初始化
     * @param format 时间格式   yyyyMMddHHmmssSSS
     * @return
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:45
     */
    public IDUtil(String format) {
        this.sfd = new SimpleDateFormat(format);
        this.dateFlag = this.sfd.format(new Date());
        this.sequence = 0;
    }

    /**
     *   初始化
     * @param sequence 开始累加数字
     * @return
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:45
     */
    public IDUtil(int sequence) {
        this.sequence = sequence;
    }

    public String generatedNoByFormatDate(){
        return this.generatedNoByFormatDate(null);
    }

    public String generatedNoByFormatDateAndNum(int digits){
        return this.generatedNoByFormatDateAndNum(digits, null);
    }

    public String generatedNoByFormatDateAndRandom(int randomDigits){
        return this.generatedNoByFormatDateAndRandom(randomDigits, null);
    }

    public String generatedNoByPrefix(String prefix, int digits){
        return this.generatedNoByPrefix(prefix, digits, null);
    }

    public String generatedNoByDateAndPrefix(String prefix){
        return this.generatedNoByDateAndPrefix(prefix, null);
    }

    public String generatedNoBySuffix(String suffix, int digits){
        return this.generatedNoBySuffix(suffix, digits, null);
    }

    public String generatedNoByDateAndSuffix(String suffix){
        return this.generatedNoByDateAndSuffix(suffix, null);
    }

    /**
     *   根据时间获取编号
     * @param machine 机器码
     * @return java.lang.String
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:46
     */
    public String generatedNoByFormatDate(String machine){
        String formatDate = this.sfd.format(new Date());
        if (Objects.nonNull(machine)){
            return formatDate + machine;
        }
        return formatDate;
    }

    /**
     *   根据时间+机器码+后面累加的位数组成的编号
     * @param machine 机器码
     * @param digits 累加值的位数
     * @return java.lang.String
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:46
     */
    public String generatedNoByFormatDateAndNum(int digits, String machine){
        String no = "";
        String formatDate = this.sfd.format(new Date());

        if (this.dateFlag.equals(formatDate)){
            this.sequence ++;
        }else {
            this.sequence = 0;
        }

        if (Objects.nonNull(machine)){
            no = formatDate + machine + String.format("%0"+digits+"d", this.sequence);
        }else {
            no = formatDate + String.format("%0"+digits+"d", this.sequence);
        }

        return no;
    }

    /**
     *   根据时间+机器码+后面随机数组成的编号
     * @param machine 机器码
     * @param randomDigits 随机数的位数
     * @return java.lang.String
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:46
     */
    public String generatedNoByFormatDateAndRandom(int randomDigits, String machine){
        String no = "";
        String formatDate = this.sfd.format(new Date());
        StringBuffer sb = new StringBuffer("9");
        for (int i = 1; i < randomDigits; i++) {
            sb.append("9");
        }
        Random random = new Random();

        int randomNum = 0;
        do {
            randomNum = random.nextInt(Integer.valueOf(sb.toString()));
        }while (this.randomSet.contains(randomNum));
        randomSet.add(randomNum);

        if (Objects.nonNull(machine)){
            no = formatDate + machine + String.format("%0"+randomDigits+"d", randomNum);
        }else {
            no = formatDate + String.format("%0"+randomDigits+"d", randomNum);
        }
        return no;
    }

    /**
     *   根据前缀+累加位数
     * @param prefix 前缀
     * @param machine 机器码(可省略)
     * @param digits 随机数的位数
     * @return java.lang.String
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:46
     */
    public String generatedNoByPrefix(String prefix, int digits, String machine){
        String no = "";
        this.sequence ++;

        if (Objects.nonNull(machine)){
            no = prefix + machine + String.format("%0"+digits+"d", this.sequence);
        }else {
            no = prefix + String.format("%0"+digits+"d", this.sequence);
        }
        return no;
    }

    /**
     *   根据前缀+时间
     * @param prefix 后缀
     * @param machine 机器码(可省略) null
     * @return java.lang.String
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:46
     */
    public String generatedNoByDateAndPrefix(String prefix, String machine){
        String no = "";
        String formatDate = this.sfd.format(new Date());

        if (Objects.nonNull(machine)){
            no = prefix + formatDate + machine;
        }else {
            no = prefix + formatDate;
        }
        return no;
    }

    /**
     *   根据后缀+累加位数
     * @param suffix 后缀
     * @param machine 机器码(可省略) null
     * @param digits 随机数的位数
     * @return java.lang.String
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:46
     */
    public String generatedNoBySuffix(String suffix, int digits, String machine){
        String no = "";
        this.sequence ++;

        if (Objects.nonNull(machine)){
            no = machine + String.format("%0"+digits+"d", this.sequence) + suffix;
        }else {
            no = String.format("%0"+digits+"d", this.sequence) + suffix;
        }
        return no;
    }

    /**
     *   根据后缀+时间
     * @param suffix 后缀
     * @param machine 机器码(可省略) null
     * @return java.lang.String
     * @throws
     * @author Surpass
     * @date 2021/6/15 16:46
     */
    public String generatedNoByDateAndSuffix(String suffix, String machine){
        String no = "";
        String formatDate = this.sfd.format(new Date());

        if (Objects.nonNull(machine)){
            no = formatDate + machine + suffix;
        }else {
            no = formatDate + suffix;
        }
        return no;
    }
}
